package com.igeek.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.igeek.ssm.common.CommonResult;
import com.igeek.ssm.pojo.User;
import com.igeek.ssm.service.UserService;
import com.igeek.ssm.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @Description TODO
 * @Author chenmin
 * @Version 1.0
 * @Date 2023/1/12 15:35
 *
 * 1.@RequestBody注解，将json数据，转成Java对象
 *
 * 2.@ResponseBody注解，将Java对象，转成json数据
 * @RestController 等价于 @Controller+@ResponseBody 。相当于类中每一个方法的返回值都是json数据格式
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired(required = false)
    private UserService userService;

    //登录  处理/user/login请求
    @PostMapping("login")
    public CommonResult<User> login(@RequestBody LoginVO vo){
        User user = userService.login(vo);
        CommonResult<User> commonResult = null;
        if(user!=null){
            commonResult = new CommonResult<>(200 , "登录成功" , user);
        }else{
            commonResult = new CommonResult<>(455 , "登录失败");
        }
        return commonResult;
    }

    //注册  MultipartFile file处理上传的图片信息
    @PostMapping("register")
    public CommonResult register(User user , MultipartFile file) throws IOException {
        if(file!=null){
            //oldName  user1.png
            String oldName = file.getOriginalFilename();
            System.out.println("oldName = "+oldName);
            if(StringUtils.hasLength(oldName)){
                String newName = UUID.randomUUID()+oldName.substring(oldName.lastIndexOf("."));

                //给图片服务器写出图片
                file.transferTo(new File("G:\\temp\\img\\"+newName));
                //给user对象设置图片
                user.setPic("/img/"+newName);
            }
        }

        CommonResult commonResult = null;
        if(user!=null){
            boolean flag = userService.register(user);
            if(flag){
                commonResult = new CommonResult(200 , "注册成功" , flag);
            }else{
                commonResult = new CommonResult(455 , "注册失败");
            }
        }
        return commonResult;
    }

    //查询用户列表
    @GetMapping("viewAll")
    public CommonResult viewAll(
            @RequestParam(value = "current" , required = false , defaultValue = "1") int current ,
            @RequestParam(value = "query" , required = false , defaultValue = "") String query
    ){
        //查询用户列表
        List<User> users = userService.viewAllByQuery(current, query);
        CommonResult commonResult = null;
        if(users!=null && users.size()!=0){
            //封装PageInfo对象
            PageInfo<User> pageInfo = new PageInfo<>(users);
            //将PageInfo、查询条件一起封装至Map集合中
            HashMap<String,Object> map = new HashMap<>();
            map.put("pageInfo",pageInfo);
            map.put("query",query);
            commonResult = new CommonResult(200 , "查询成功" , map);
        }else{
            commonResult = new CommonResult(477 , "查询失败");
        }
        return commonResult;
    }

    //删除用户信息  @PathVariable可以解析路径变量，value属性与{Key}与Key一致即可
    @DeleteMapping("delete/{id}")
    public CommonResult del(@PathVariable("id") int id){
        CommonResult commonResult = null;
        boolean flag = userService.delete(id);
        if(flag){
            commonResult = new CommonResult(200 , "删除成功" , flag);
        }else{
            commonResult = new CommonResult(499 , "删除失败");
        }
        return commonResult;
    }

    //查询单个用户信息
    @GetMapping("viewOne/{id}")
    public CommonResult viewOne(@PathVariable("id")int id){
        CommonResult commonResult = null;
        User user = userService.viewOne(id);
        if(user!=null){
            commonResult = new CommonResult(200 , "查询成功" , user);
        }else{
            commonResult = new CommonResult(455 , "查询失败");
        }
        return commonResult;
    }

    //修改用户信息   @PutMapping 更新，携带头像，处理FormData数据，注入多部件解析器CommonsMultipartResolver
    //参数绑定  User类中属性名称，与页面表单key一致
    //参数绑定  文件上传的key，与MultipartFile参数名称一致
    @PutMapping("update")
    public CommonResult update(User user , MultipartFile file) throws IOException {
        if(file!=null){
            String oldName = file.getOriginalFilename();
            if(oldName!=null && oldName.length()!=0){
                String newName = UUID.randomUUID()+oldName.substring(oldName.lastIndexOf("."));

                //给图片服务器写出图片
                file.transferTo(new File("G:\\temp\\img\\"+newName));
                //用户信息设置图片
                user.setPic("/img/"+newName);
            }
        }

        //未上传图片
        if(user.getPic()==null){
            String pic = userService.viewOne(user.getId()).getPic();
            if(StringUtils.hasLength(pic)){
                //原来有图片，则直接设置原图片
                user.setPic(pic);
            }
        }

        //更新操作
        CommonResult commonResult = null;
        boolean flag = userService.update(user);
        if(flag){
            commonResult = new CommonResult(200 , "更新成功" , flag);
        }else{
            commonResult = new CommonResult(499 , "更新失败");
        }
        return commonResult;
    }
}
